package com.xmtlxx.web.controller.wms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.web.controller.wms.service.IInventoryPlanService;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.wms.domain.bo.InventoryBo;
import com.xmtlxx.wms.domain.bo.InventoryPlanBo;
import com.xmtlxx.wms.domain.bo.InventoryPlanDeleteBo;
import com.xmtlxx.wms.domain.bo.InventoryPlanProExportBo;
import com.xmtlxx.wms.domain.vo.*;
import com.xmtlxx.wms.service.IInventoryService;
import com.xmtlxx.wms.service.ILocatorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 盘点计划Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "盘点计划控制器", tags = {"盘点计划管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/inventoryPlan")
public class InventoryPlanController extends BaseController {

    private final IInventoryPlanService iInventoryPlanService;

    private final IInventoryService iInventoryService;

    private final ILocatorService iLocatorService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<InventoryPlanVo> initBean( @RequestBody InventoryPlanBo bo){
        //可以对数据进行初始化
        InventoryPlanVo vo = new InventoryPlanVo();
        BeanUtil.copyProperties(bo,vo);
        if(!StringUtils.isEmpty(bo.getId())){
            vo = iInventoryPlanService.queryById(bo.getId());
        }else{
            vo.setStoreId(bo.getStoreId());
            List<InventoryDtlVo> items = new ArrayList<>();
            InventoryBo inBo = new InventoryBo();
            BeanUtil.copyProperties(bo,inBo);
            Long nums = 0l;
//            List<InventoryVo> list = iInventoryService.queryListWithOutPic(inBo);
//            if(list != null && list.size() > 0){
//                for(InventoryVo inv : list){
//                    if(inv.getNums() == null || inv.getNums() <=0){
//                        continue;
//                    }
//                    InventoryDtlVo item = new InventoryDtlVo();
//                    item.setStockId(inv.getId());
//                    item.setPno(inv.getPno());
//                    item.setName(inv.getName());
//                    item.setBooks(inv.getNums());
//                    item.setNums(0l);
//                    item.setResults(0l);
//                    item.setDelFlag("0");
//                    item.setStoreName(inv.getStoreName());
//                    item.setAreaName(inv.getAreaName());
//                    item.setLocatorName(inv.getLocatorName());
//                    items.add(item);
//                    nums += inv.getNums();
//                }
//            }
            vo.setBooks(nums);
            vo.setNums(0l);
            vo.setResults(0l);
            vo.setStatus("0");
            vo.setItems(items);
        }
        return R.ok(vo);
    }

    /**
     * 查询盘点计划明细列表
     */
    @ApiOperation("查询盘点计划明细列表")
    @PostMapping("/dtlList")
    R<InventoryPlanVo> dtlList(@RequestBody InventoryPlanBo bo) {
        InventoryPlanVo vo = new InventoryPlanVo();
        BeanUtil.copyProperties(bo,vo);
        vo.setStoreId(bo.getStoreId());
        List<InventoryDtlVo> items = new ArrayList<>();
        InventoryBo inBo = new InventoryBo();
        BeanUtil.copyProperties(bo,inBo);
        List<InventoryVo> list = iInventoryService.selectInventoryProList(inBo);
        Long nums = 0l;
        if(list != null && list.size() > 0){
            for(InventoryVo inv : list){
                if(inv.getNums() == null || inv.getNums() <=0){
                    continue;
                }
                InventoryDtlVo item = new InventoryDtlVo();
                item.setStockId(inv.getId());
                item.setPno(inv.getPno());
                item.setName(inv.getName());
                item.setInnums(inv.getInnums());
                item.setOutnums(inv.getOutnums());
                item.setDeals(inv.getDeals());
                item.setTnums(inv.getTnums());
                item.setEoutnums(inv.getEoutnums());
                item.setEtnums(inv.getEtnums());
                item.setBooks(inv.getNums());
                item.setNums(0l);
                item.setResults(0l);
                item.setDelFlag("0");
                item.setStoreName(inv.getStoreName());
                item.setAreaName(inv.getAreaName());
                item.setLocatorName(inv.getLocatorName());
                items.add(item);
                nums += inv.getNums();
            }
        }
        vo.setBooks(nums);
        vo.setNums(0l);
        vo.setResults(0l);
        vo.setStatus("0");
        vo.setItems(items);
        return R.ok(vo);
    }

    /**
     * 查询盘点计划列表
     */
    @ApiOperation("查询盘点计划列表")
    @SaCheckPermission("wms:inventoryPlan:list")
    @GetMapping("/list")
    public TableDataInfo<InventoryPlanVo> list(@Validated(QueryGroup.class) InventoryPlanBo bo, PageQuery pageQuery) {
        return iInventoryPlanService.queryPageList(bo, pageQuery);
    }


    /**
     * 导出盘点计划列表
     */
    @ApiOperation("导出盘点计划列表")
    @SaCheckPermission("wms:inventoryPlan:export")
    @Log(title = "盘点计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated InventoryPlanBo bo, HttpServletResponse response) {
        List<InventoryPlanVo> list = iInventoryPlanService.queryList(bo);
        ExcelUtil.exportExcel(list, "盘点计划", InventoryPlanVo.class, response);
    }

    @ApiOperation("查询需要导出的盘点计划物品列表")
    @SaCheckPermission("wms:inventoryPlan:export")
    @Log(title = "盘点计划物品", businessType = BusinessType.EXPORT)
    @PostMapping("/exportPlanPro")
    public void exportPlanPro(@Validated InventoryPlanBo bo, HttpServletResponse response) {
        List<InventoryDtlVo> list = iInventoryPlanService.queryPlanProList(bo.getId());
        ExcelUtil.exportExcel(list, "盘点计划物品", InventoryDtlVo.class, response);
    }

    @ApiOperation("导出所有盘点计划物品详细列表")
    @SaCheckPermission("wms:inventoryPlan:export")
    @Log(title = "盘点总表表", businessType = BusinessType.EXPORT)
    @PostMapping("/getInventoryPlanExport")
    public void getInventoryPlanExport(@Validated InventoryPlanProExportBo bo, HttpServletResponse response) {
        List<InventoryPlanProExportVo> list = iInventoryPlanService.getInventoryPlanExportList(bo);
        ExcelUtil.exportExcel(list, "盘点总表", InventoryPlanProExportVo.class, response);
    }

    @ApiOperation("导出盘点计划物品统计详细列表")
    @SaCheckPermission("wms:inventoryPlan:export")
    @Log(title = "盘点总表表", businessType = BusinessType.EXPORT)
    @PostMapping("/getInventoryPlanStatisticsExport")
    public void getInventoryPlanStatisticsExport(@Validated InventoryPlanProExportBo bo, HttpServletResponse response) {
        List<InventoryPlanStatisticsExportVo> list = iInventoryPlanService.getInventoryPlanStatisticsExportList(bo);
        ExcelUtil.exportExcel(list, "盘点总表", InventoryPlanStatisticsExportVo.class, response);
    }

    /**
     * 获取盘点计划详细信息
     */
    @ApiOperation("获取盘点计划详细信息")
    @SaCheckPermission("wms:inventoryPlan:query")
    @GetMapping("/{id}")
    public R<InventoryPlanVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        InventoryPlanVo vo = iInventoryPlanService.queryById(id);
        if(StringUtils.isNotEmpty(vo.getLocators())){
            List<String> locatorList = Arrays.asList(vo.getLocators().split(","));
            List<String> locatorName = locatorList.stream().map(item->{
                LocatorVo locatorVo = iLocatorService.queryById(item);
                return locatorVo.getName();
            }).collect(Collectors.toList());
            vo.setLocatorName(locatorName);
        }

        return R.ok(vo);
    }

    /**
     * 新增盘点计划
     */
    @ApiOperation("新增盘点计划")
    @SaCheckPermission("wms:inventoryPlan:add")
    @Log(title = "盘点计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<InventoryPlanVo> add(@Validated(AddGroup.class) @RequestBody InventoryPlanVo vo) {
        InventoryPlanBo bo = new InventoryPlanBo();
        BeanUtil.copyProperties(vo,bo);
        return R.ok(iInventoryPlanService.insertByBo(bo));
    }

    /**
     * 修改盘点计划
     */
    @ApiOperation("修改盘点计划")
    @SaCheckPermission("wms:inventoryPlan:edit")
    @Log(title = "盘点计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody InventoryPlanVo vo) {
        InventoryPlanBo bo = new InventoryPlanBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iInventoryPlanService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 修改盘点计划
     */
    @ApiOperation("修改盘点计划状态")
    @SaCheckPermission("wms:inventoryPlan:edit")
    @Log(title = "盘点计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateStatus")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> editStatus(@Validated(EditGroup.class) @RequestBody InventoryPlanVo vo) {
        InventoryPlanBo bo = new InventoryPlanBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iInventoryPlanService.updateStatusByBo(bo) ? 1 : 0);
    }

    /**
     * 删除盘点计划
     */
    @ApiOperation("删除盘点计划")
    @SaCheckPermission("wms:inventoryPlan:remove")
    @Log(title = "盘点计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iInventoryPlanService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 删除盘点计划内物品
     */
    @ApiOperation("删除盘点计划内物品")
    @PostMapping("/removeDtl")
    public R<Void> removeDtl(@RequestBody InventoryPlanDeleteBo bo){
        return toAjax(iInventoryPlanService.deletePlanProByPnos(bo) ? 1 : 0);
    }
}
